package com.loongtech.bi.action.authManagement;

import com.loongtech.bi.entity.system.EntitySysFunction;
import com.loongtech.bi.entity.system.EntitySysModel;
import com.loongtech.bi.manager.system.SysFunctionManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysModelManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Author: yuri
 * Description: 系统模块
 * Date: 18:22 2017/5/2
 */
@RestController
@RequestMapping("/model")
@Scope("prototype")
public class SysModelAction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SysModelManager sysModelManager;
    @Resource
    private SysFunctionManager sysFunctionManager;

    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> list() {
        List<EntitySysModel> list;
        try {
            list = sysFunctionManager.findByQuery("from EntitySysModel order by id asc");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> add(EntitySysModel model) {
        // 判断参数
        String name = model.getName();
        int fatherId = model.getFatherId();
        try {
            if (fatherId > 0 && null == sysModelManager.get(fatherId)) {
                return new ResultData<>(RetCode.eFatherEmpty.getErrorcode(), RetCode.eFatherEmpty.getErrorMsg());
            }
            // 判断用户name是否已经被注册
            if (null != sysModelManager.getBy("name", name)) {
                return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
            }
            if (null != sysModelManager.getBy("id", model.getId())) {
                return new ResultData<>(RetCode.eIdExist.getErrorcode(), RetCode.eIdExist.getErrorMsg());
            }
            // 注册模块
            sysModelManager.persist(model);
            sysLogManager.addLog(session.getUserName(), "增加模块成功:" + name);
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(EntitySysModel model) {
        try {
            EntitySysModel cur = sysModelManager.get(model.getId());
            if (cur != null) {
                sysModelManager.merge(model);
                sysLogManager.addLog(session.getUserName(), "模块修改成功!modelId=" + model.getId());
                return ResultData.success();
            } else {
                sysLogManager.addLog(session.getUserName(), "模块修改失败,不存在模块modelId=" + model.getId());
                return new ResultData<>(RetCode.eModuleEmpty.getErrorcode(), RetCode.eModuleEmpty.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "delete.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delete(@RequestParam(value = "id") Integer id) {
        try {
            List<EntitySysFunction> list = sysFunctionManager.findAll(EntitySysFunction.K.modelId, id);
            List<EntitySysModel> listParent = sysModelManager.findAll("fatherId", id);
            if (null != list && !list.isEmpty() || null != listParent && !listParent.isEmpty()) {
                return new ResultData<>(RetCode.eRelationExist.getErrorcode(), RetCode.eRelationExist.getErrorMsg());
            }
            if (null != sysModelManager.get(id)) {
                sysModelManager.removeById(id);
                return ResultData.success();
            }
            return new ResultData<>(RetCode.eModuleEmpty.getErrorcode(), RetCode.eModuleEmpty.getErrorMsg());
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}